﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Configuration;
using System.ComponentModel.DataAnnotations;
using TracNghiem.Models;
using System.Collections.ObjectModel;
using System.Web.Mvc;
using System.Data.OracleClient;

namespace TracNghiem.Areas.Admin.Models
{
   
    public class AccountModel
    {
        public string id { get; set; }
        [Required(ErrorMessage = "Username không được để trống!")]
        public string username { get; set; }
        [Required(ErrorMessage = "Password không được để trống!")]
        public string password { get; set; }
        public bool RememberMe { get; set; }
        public int checkAccount(AccountModel Account)
        { 
            string sql = "SELECT * FROM nguoi_dung WHERE user_name='" + Account.username + "' and password ='" + Account.password + "' and trang_thai=1";
            return General.Lib_CheckExist(sql);
        }
    }
    public class UserModel
    {
        public string id { get; set; }
        [Required(ErrorMessage = "Tên đăng nhập không được để trống!")]
        public string  username{ get; set; }
        [Required(ErrorMessage = "Mật khẩu không được để trống!")]
        public string  password{ get; set; }
        [Required(ErrorMessage = "Họ tên không được để trống!")]
        public string hoten { get; set; }
        public string donvi_id { get; set; }
        public string trang_thai { get; set; }
        public string so_dien_thoai { get; set; }
        public string mail { get; set; }
        public string ten_trung_tam { get; set; }
        public SelectList trungtam_id { get; set; }
        public List<UserModel> getUser()
        {
            List<UserModel> user_ = new List<UserModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT a.*,b.ten_don_vi ten_trung_tam FROM nguoi_dung a,donvi_view b where a.donvi_id=b.id order by b.id,a.user_name");
            while (odr.Read())
                user_.Add(new UserModel 
                { 
                    id = odr["id"].ToString(), 
                    username = odr["user_name"].ToString(), 
                    password = odr["password"].ToString(),
                    hoten = odr["ho_ten"].ToString(),
                    donvi_id = odr["donvi_id"].ToString(),
                    trang_thai = odr["trang_thai"].ToString()=="1"?"Bình thường":"Khóa",
                    so_dien_thoai = odr["so_dien_thoai"].ToString(),
                    mail = odr["mail"].ToString(),
                    ten_trung_tam = odr["ten_trung_tam"].ToString(),
                });
            return user_;
        }
        public void addUser(UserModel us)
        {
            string sql = "INSERT INTO nguoi_dung";
            sql += " select nvl((select max(id)+1 from nguoi_dung),1),'" + us.username + "','" + us.password + "'," + us.donvi_id + "," + us.trang_thai + ",'" + us.hoten + "','" + us.so_dien_thoai + "','" + us.mail + "' from dual";
            General.Lib_ExecuteNonQuery(sql);
        }
        public void updateUser(UserModel us)
        {
            string sql = "UPDATE nguoi_dung SET user_name='" + us.username + "', password='" + us.password + "', donvi_id=" + us.donvi_id + ", trang_thai=" + us.trang_thai + ",ho_ten='" + us.hoten + "', so_dien_thoai='" + us.so_dien_thoai + "', mail='" + us.mail + "' where id='" + us.id + "'";
            General.Lib_ExecuteNonQuery(sql);
        }
        public void deleteUser(int id)
        {
            string sql = "DELETE FROM nguoi_dung WHERE id=" + id + "";
            General.Lib_ExecuteNonQuery(sql);
        }
        public int checkExist(UserModel us)
        {
            string sql = "SELECT * FROM nguoi_dung WHERE user_name='" + us.username + "'";
            return General.Lib_CheckExist(sql);
        }
        
    }
}